﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIS.UI;
using SIS.ENT;
using SIS.BLL;
using System.Globalization;

public partial class Employee_EmployeeSalaryTrans_Entry : System.Web.UI.Page
{
    public Common _common = new Common();
    public long _EmployeeSalaryTransKey = -1;
    public Int32 _SchoolKey = -1;
    protected void Page_Load(object sender, EventArgs e)
    {

        lblTopMessage.Text = DateTime.Now.ToString();

        if (!Page.IsPostBack)
        {
            if (Request.Params["SchoolKey"] != null)
            {
                _SchoolKey = Convert.ToInt32(Request.Params["SchoolKey"].ToString());
            }
            _common.LoadCurrencyCodeDDL(ddlCurrency, string.Empty);
            _common.LoadEmployeeDDL(ddlEmployee, _SchoolKey, string.Empty);
            _common.LoadPaymentTypeCodeCodeDDL(ddlpaymentType, string.Empty);

            if (Request.Params["EmployeeSalaryTransKey"] != null)
            {

                if (long.TryParse(Request.Params["EmployeeSalaryTransKey"].ToString(), out _EmployeeSalaryTransKey) && Int32.TryParse(Request.Params["SchoolKey"].ToString(), out _SchoolKey))
                {
                    LoadEmployeeSalaryTrans(_EmployeeSalaryTransKey);
                }
                else
                {
                    lblTopMessage.Text = "Invalid Employee Salary Trans ID.";
                }

                lblTopMessage.Text = DateTime.Now.ToString();
            }
            else
            {

                ClearSalaryTransRecord();
            }
        }

    }

    private void ClearSalaryTransRecord()
    {
        ddlCurrency.SelectedIndex = 0;
        ddlpaymentType.SelectedIndex = 0;
        ddlEmployee.SelectedIndex = 0;
        txtAmount.Text = string.Empty;
        txtOthersAmount.Text = string.Empty;
        txtAdditionalAmount.Text = string.Empty;
        txtTransDate.Text = string.Empty;
        txtRemarks.Text = string.Empty;
        lblSalaryTransKey.Text = "-1";
        lblUserAccountKey.Text = "-1";
    }

    private void LoadEmployeeSalaryTrans(long SalaryTransKey)
    {
        EmployeeSalaryTransTable _objEmployeeSalaryTransKey = new EmployeeSalaryTransTable();
        _objEmployeeSalaryTransKey = EmployeeSalaryTransTableManager.GetItem(SalaryTransKey);

        if (_objEmployeeSalaryTransKey == null)
        {
            lblTopMessage.Text = "Employee account ID (" + SalaryTransKey.ToString() + ") is not found!";
        }
        else if (_objEmployeeSalaryTransKey.UserAccountKey <= 0)
        {
            lblTopMessage.Text = "Employee account ID is not found!";
        }
        lblUserAccountKey.Text = _objEmployeeSalaryTransKey.UserAccountKey.ToString();
        lblSalaryTransKey.Text = SalaryTransKey.ToString();

        ddlEmployee.SelectedValue = Convert.ToString(_objEmployeeSalaryTransKey.UserAccountKey);
        ddlpaymentType.SelectedValue = Convert.ToString(_objEmployeeSalaryTransKey.PaymentTypeCode);
        ddlCurrency.SelectedValue = Convert.ToString(_objEmployeeSalaryTransKey.CurrencyCode);
        txtTransDate.Text = _objEmployeeSalaryTransKey.TransactionDate.ToString("dd/MM/yyyy");
        txtAmount.Text = _objEmployeeSalaryTransKey.Amount.Equals(0) ? "" : _objEmployeeSalaryTransKey.Amount.ToString("0.00");
        txtAdditionalAmount.Text = _objEmployeeSalaryTransKey.AdditionalAmount.Equals(0) ? "" : _objEmployeeSalaryTransKey.AdditionalAmount.ToString("0.00");
        txtOthersAmount.Text = _objEmployeeSalaryTransKey.OthersAmount.Equals(0) ? "" : _objEmployeeSalaryTransKey.OthersAmount.ToString("0.00");
        txtRemarks.Text = _objEmployeeSalaryTransKey.Remarks;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";

        Page.Validate("EmployeeSalaryTrans");
        if (Page.IsValid)
        {
            EmployeeSalaryTransTable _objEmployeeSalaryTrans = new EmployeeSalaryTransTable();
            _objEmployeeSalaryTrans.EmployeeSalaryTransKey = Convert.ToInt64(lblSalaryTransKey.Text);
            _objEmployeeSalaryTrans.UserAccountKey = Convert.ToInt32(ddlEmployee.SelectedValue);
            _objEmployeeSalaryTrans.PaymentTypeCode = Convert.ToInt16(ddlpaymentType.SelectedValue);
            _objEmployeeSalaryTrans.CurrencyCode = Convert.ToInt16(ddlCurrency.SelectedValue);
            string TransDate = txtTransDate.Text;
            var dtfi1 = new DateTimeFormatInfo { ShortDatePattern = "dd-MM-yyyy HH:mm:ss", DateSeparator = "-" };
            DateTime objStartDate = Convert.ToDateTime(TransDate, dtfi1);
            _objEmployeeSalaryTrans.TransactionDate = objStartDate;
            _objEmployeeSalaryTrans.Amount = Convert.ToDecimal(txtAmount.Text);
            _objEmployeeSalaryTrans.OthersAmount = txtOthersAmount.Text.Equals(string.Empty) ? 0 : Convert.ToDecimal(txtOthersAmount.Text);
            _objEmployeeSalaryTrans.AdditionalAmount = txtAdditionalAmount.Text.Equals(string.Empty) ? 0 : Convert.ToDecimal(txtAdditionalAmount.Text);
            _objEmployeeSalaryTrans.Remarks = txtRemarks.Text;
            _objEmployeeSalaryTrans.Status = 1;
            _objEmployeeSalaryTrans.CreatedDateTime = DateTime.Now;
            _objEmployeeSalaryTrans.ModifiedDateTime = DateTime.Now;
            _objEmployeeSalaryTrans.DateOfBirth = DateTime.Now;

            long lResult = EmployeeSalaryTransTableManager.Save(_objEmployeeSalaryTrans);
            lblSalaryTransKey.Text = lResult.ToString();
            if (lResult > 0)
            {
                lblMessage.Text = "Saved Successfully";
            }
            else
            {
                lblMessage.Text = "Saved Fail";
            }

        }
    }

    protected void btnAddNewEmployee_Click(object sender, EventArgs e)
    {
        ClearSalaryTransRecord();
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("EmployeeSalaryTrans-List.aspx");
    }

    protected void SalaryTransDate_ServerSideValidation(object source, ServerValidateEventArgs args)
    {

        bool _retValue = true;
        string TransDate = txtTransDate.Text;

        if (TransDate.Equals(string.Empty))
        {

            _retValue = false;

        }

        args.IsValid = _retValue;
    }
}